package grape.workflow.rest.process.mvc;

import cn.hutool.core.util.StrUtil;
import grape.common.exception.runtime.InvalidParamsException;
import grape.common.rest.mvc.SuperController;
import grape.workflow.rest.process.form.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.flowable.engine.FormService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.form.api.FormInfo;
import org.flowable.form.model.SimpleFormModel;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by yangwei
 * Created at 2020/6/28 18:59
 */
@RestController
@RequestMapping("/workflow/process")
@Api(tags = "流程相关接口")
public class ProcessController extends SuperController {


    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private FormService formService;
    @Autowired
    protected org.flowable.form.api.FormService apiFormService;

    @ApiOperation("带业务编号的流程启动接口")
    @PreAuthorize("hasAuthority('process:startWorkflow')")
    @PostMapping(value = "/startWorkflow")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean startWorkflow(@RequestBody @Valid StartWorkflowForm startWorkflowForm) {
        if (StrUtil.isAllEmpty(startWorkflowForm.getProcessDefinitionId(),startWorkflowForm.getProcessDefinitionKey())) {
            throw new InvalidParamsException("流程定义id或流程定义key不能都为空");
        }
        if (!isStrEmpty(startWorkflowForm.getProcessDefinitionId())) {
            runtimeService.startProcessInstanceById(startWorkflowForm.getProcessDefinitionId(), startWorkflowForm.getBusinessKey());
        } else
        if (!isStrEmpty(startWorkflowForm.getProcessDefinitionKey())) {
            runtimeService.startProcessInstanceByKey(startWorkflowForm.getProcessDefinitionId(), startWorkflowForm.getBusinessKey());
        }
        return true;
    }

    @ApiOperation("动态表单的流程启动接口")
    @PreAuthorize("hasAuthority('process:dynamicForm:startWorkflowWithForm')")
    @PostMapping(value = "/dynamicForm/startWorkflowWithForm")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean dynamicFormStartWorkflowWithForm(@RequestBody @Valid DynamicFormStartWorkflowWithFormForm startWorkflowForm) {
        // 注意这里没有用runtime的 startProcessInstanceWithForm 因为这是内部表单的方式，在有formkey的情况下才可以，动态表单不适合
        formService.submitStartFormData(startWorkflowForm.getProcessDefinitionId(),startWorkflowForm.getVariables());
        return true;
    }
    @ApiOperation("关联/外置表单的流程启动接口")
    @PreAuthorize("hasAuthority('process:relationForm:startWorkflowWithForm')")
    @PostMapping(value = "/relationForm/startWorkflowWithForm")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean relationFormStartWorkflowWithForm(@RequestBody @Valid RelationFormStartWorkflowWithFormForm startWorkflowForm) {
        runtimeService.startProcessInstanceWithForm(startWorkflowForm.getProcessDefinitionId(),null,startWorkflowForm.getVariables(),null);
        return true;
    }

}
